/** 
 * Copyright 2011 Noranj LTD
 *
 */

package com.noranj.web.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.noranj.web.client.UserManagment;
import com.noranj.web.shared.UserAccountDTO;

public class UserManagmentImpl extends RemoteServiceServlet implements UserManagment{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();

        String thisURL = req.getRequestURI();

        resp.setContentType("text/html");
        try {
			if (req.getUserPrincipal() != null) {
			    resp.getWriter().println("<p>Hello, " +
			                             req.getUserPrincipal().getName() +
			                             "!  You can <a href=\"" +
			                             userService.createLogoutURL(thisURL) +
			                             "\">sign out</a>.</p>");
			} else {
			    resp.getWriter().println("<p>Please <a href=\"" +
			                             userService.createLoginURL(thisURL) +
			                             "\">sign in</a>.</p>");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
		
	public String login(String urlPath)   {
		UserAccountDTO userAccountDTO = new UserAccountDTO();
		HttpServletRequest req = this.getThreadLocalRequest();
		HttpServletResponse resp = this.getThreadLocalResponse();
		UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
		String urlRedirect = "";
	
	        if (user != null) {
	        	userAccountDTO.setEmailAddress(user.getEmail());
	        	userAccountDTO.setId(user.getUserId());
	        	userAccountDTO.setName(user.getNickname());
	        } else {
	        	urlRedirect = userService.createLoginURL(urlPath);
	        }
			return urlRedirect;
	}
		 
	public String logout(String urlPath) {
		HttpServletRequest req = this.getThreadLocalRequest();
		HttpServletResponse resp = this.getThreadLocalResponse();
		String urlRedirect = req.getRequestURI();
		UserService userService = UserServiceFactory.getUserService();
	    getThreadLocalRequest().getSession().invalidate();
	    urlRedirect = userService.createLogoutURL(urlPath);
	    return urlRedirect;
	  }
		
	public List<UserAccountDTO>  userAccountInfo()   {
		List<UserAccountDTO> userAccountDTOs = new ArrayList<UserAccountDTO>() ;
		UserAccountDTO userAccountDTO = new UserAccountDTO();
		HttpServletRequest req = this.getThreadLocalRequest();
		HttpServletResponse resp = this.getThreadLocalResponse();
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
    	
        if (user != null) {
        	
        	userAccountDTO.setEmailAddress(user.getEmail());
        	userAccountDTO.setId(user.getUserId());
        	userAccountDTO.setName(user.getNickname());
        	userAccountDTOs.add( userAccountDTO);
        	//resp.setContentType("text/plain");
            //resp.getWriter().println("Hello, " + user.getNickname());
        }

		return userAccountDTOs;

		
}
}
